package com.dingwen.treasure.business.controller;

import com.dingwen.treasure.base.controller.BaseViewController;
import com.dingwen.treasure.base.util.OptimizeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 　OptimizeUtilController: 优化工具测试
 * 　@author dingwen
 * 　@since  2022/8/28
 */
@Api(tags = "优化工具API")
@RestController
@Slf4j
@RequestMapping("optimize")
@RequiredArgsConstructor
public class OptimizeUtilController implements BaseViewController {

    @ApiOperation(value = "API使用测试")
    @GetMapping
    public void test() {
        optimizeApi();
    }

    @SneakyThrows(Throwable.class)
    private void optimizeApi() {
        OptimizeUtils.start("任务1");
        TimeUnit.SECONDS.sleep(1);
        OptimizeUtils.stop("任务1");
        for (int i = 0; i < 100; i++) {
            OptimizeUtils.start("任务2");
            for (int j = 0; j < 1000; j++) {
                OptimizeUtils.start("任务2-1");
                OptimizeUtils.stop("任务2-1");
            }
            OptimizeUtils.stop("任务2");
        }

        OptimizeUtils.print("任务2",log);
        OptimizeUtils.print(log);
    }
}
